package com.qs.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.infomanage.util.TimeUtil;
import com.qs.enums.PeriodOption;

public class PeriodUtil {
	
	public static Period getPeriod(PeriodOption periodOption) {
		Period period = new Period();
		String curYear = TimeUtil.showCurrentYear();
		String curMonth = getCurrentMonth();
		String prevMonth = getPrevMonth();
		switch (periodOption) {
		case ALL:
			period.setAll(true);
			break;
		case CUR_YEAR:
			period.setBegin(curYear + "-01-01");
			period.setEnd(curYear + "-12-31");
			break;
		case FIRSTHALF_YEAR:
			period.setBegin(curYear + "-01-01");
			period.setEnd(curYear + "-06-30");
			break;
		case LASTHALF_YEAR:
			period.setBegin(curYear + "-07-01");
			period.setEnd(curYear + "-12-31");
			break;
		case CUR_QUARTER:
			setPeriodQuarter(period);
			break;
		case PREV_MONTH:
			if(curMonth.equals("01")) {
				String prevYear = String.valueOf(Integer.parseInt(curYear) - 1);
				period.setBegin(prevYear + "-" + prevMonth + "-01");
				period.setEnd(prevYear + "-" + prevMonth + "-31");
			}
			period.setBegin(curYear + "-" + prevMonth + "-01");
			period.setEnd(curYear + "-" + prevMonth + "-31");
			break;
		case CUR_MONTH:
			period.setBegin(curYear + "-" + curMonth + "-01");
			period.setEnd(curYear + "-" + curMonth + "-31");
			break;
		case PREV_WEEK:
			String[] prevWeekDays = gePrevWeekDays();
			period.setBegin(prevWeekDays[0]);
			period.setEnd(prevWeekDays[prevWeekDays.length - 1]);
			break;
		case CUR_WEEK:
			String[] weekDays = getWeekDate(TimeUtil.showCurrentDay());
			period.setBegin(weekDays[0]);
			period.setEnd(weekDays[weekDays.length - 1]);
			break;
		case TODAY:
			period.setBegin(TimeUtil.showCurrentDay());
			period.setEnd(TimeUtil.showCurrentDay());
			break;
		case RECENT_THREE_DAYS:
			String[] recentThreeDays = getRecentTreeDays();
			period.setBegin(recentThreeDays[0]);
			period.setEnd(recentThreeDays[1]);
			break;
//		case CUSTOM:
//			break;
		default:
			break;
		}
		return period;
	}
	
	private static String getCurrentMonth() {
		String yearMonthDay = TimeUtil.showCurrentDay();
		return yearMonthDay.substring(5, 7);
	}
	
	private static String getPrevMonth() {
		String curYear = TimeUtil.showCurrentYear();
		String curMonth = getCurrentMonth();
		if(curMonth.equals("01")) {
//			String prevYear = String.valueOf(Integer.parseInt(curYear) - 1);
			return "12";
		}else if(curMonth.equals("02")) {
			return "01";
		}else if(curMonth.equals("03")) {
			return "02";
		}else if(curMonth.equals("04")) {
			return "03";
		}else if(curMonth.equals("05")) {
			return "04";
		}else if(curMonth.equals("06")) {
			return "05";
		}else if(curMonth.equals("07")) {
			return "06";
		}else if(curMonth.equals("08")) {
			return "07";
		}else if(curMonth.equals("09")) {
			return "08";
		}else if(curMonth.equals("10")) {
			return "09";
		}else if(curMonth.equals("11")) {
			return "10";
		}else if(curMonth.equals("12")) {
			return "11";
		}
		return null;
	}
	
	private static void setPeriodQuarter(Period period){
		String month = getCurrentMonth();
		if(month.equals("01") || month.equals("02") || month.equals("03")) {
			period.setBegin(TimeUtil.showCurrentYear() + "-01-01");
			period.setEnd(TimeUtil.showCurrentYear() + "-03-01");
		}else if(month.equals("04") || month.equals("05") || month.equals("06")){
			period.setBegin(TimeUtil.showCurrentYear() + "-04-01");
			period.setEnd(TimeUtil.showCurrentYear() + "-06-01");
		}else if(month.equals("07") || month.equals("08") || month.equals("09")) {
			period.setBegin(TimeUtil.showCurrentYear() + "-07-01");
			period.setEnd(TimeUtil.showCurrentYear() + "-09-01");
		}else if(month.equals("10") || month.equals("11") || month.equals("12")) {
			period.setBegin(TimeUtil.showCurrentYear() + "-10-01");
			period.setEnd(TimeUtil.showCurrentYear() + "-12-01");
		}
	}
	
	public static String[] getWeekDate(String date){
		String[] weeks = new String[7];//返回的这周的日期
		String[] a = date.split("-");
		int week = getDayOfWeek(a[0], a[1], a[2]);//获取周几
		int minWeek = 0;
		int maxWeek = 7;
		String format = "yyyy-MM-dd";
		
		if(week == 1){//如果是周日（老外是从周日开始算一周，所以有点恶心）
			weeks[6] = date;
			for(int i = 5; i >= 0; i--){
				weeks[i] = getFormatDateAdd(getStrToDate(date, format), -1, format);
				date = weeks[i];
			}
		}else{
			int temp = week - 2;
			weeks[temp] = date;
			for(int i = temp - 1; i >= minWeek; i--){
				weeks[i] = getFormatDateAdd(getStrToDate(date, format), -1, format);
				date = weeks[i];
			}
			date = weeks[temp];
			for(int i = temp + 1; i < maxWeek; i++){
				weeks[i] = getFormatDateAdd(getStrToDate(date, format), 1, format);
				date = weeks[i];
			}
		}
		
		return weeks;
	}
	
	/**
	 * 根据指定的年、月、日返回当前是星期几。1表示星期天、2表示星期一、7表示星期六。
	 * 
	 * @param year
	 * @param month
	 *            month是从1开始的12结束
	 * @param day
	 * @return 返回一个代表当期日期是星期几的数字。1表示星期天、2表示星期一、7表示星期六。
	 */
	public static int getDayOfWeek(String year, String month, String day) {
		Calendar cal = new GregorianCalendar(new Integer(year).intValue(),
				new Integer(month).intValue() - 1, new Integer(day).intValue());
		return cal.get(Calendar.DAY_OF_WEEK);
	}
	
	/**
	 * 取得给定日期加上一定天数后的日期对象.
	 * 
	 * @param date
	 *            给定的日期对象
	 * @param amount
	 *            需要添加的天数，如果是向前的天数，使用负数就可以.
	 * @param format
	 *            输出格式.
	 * @return Date 加上一定天数以后的Date对象.
	 */
	public static String getFormatDateAdd(Date date, int amount, String format) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(GregorianCalendar.DATE, amount);
		return getFormatDateTime(cal.getTime(), format);
	}
	
	/**
	 * 根据给定的格式与时间(Date类型的)，返回时间字符串。最为通用。<br>
	 * 
	 * @param date
	 *            指定的日期
	 * @param format
	 *            日期格式字符串
	 * @return String 指定格式的日期字符串.
	 */
	public static String getFormatDateTime(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	/**
	 * 返回制定日期字符串的date格式
	 * 
	 * @param date
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date getStrToDate(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private static String[] gePrevWeekDays() {
		Date startDate = null;
		Date endDate = null;
		Calendar c = Calendar.getInstance();// 当天
		c.add(Calendar.WEEK_OF_MONTH, -1);// 上一周的同一天

		if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			c.add(Calendar.DAY_OF_YEAR, -1);
		}

		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 上周一
		startDate = c.getTime();

		c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);// 上同六
		c.add(Calendar.DAY_OF_WEEK, 1);// 上周日
		endDate = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.format(endDate.getTime());
		return new String[] {
				sdf.format(startDate.getTime()),
				sdf.format(endDate.getTime())
		};
	}
	
	private static String[] getRecentTreeDays() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		String today = sdf.format(cal.getTime());
		cal.add(cal.DATE, -2);
		String prevprevDay = sdf.format(cal.getTime());
		return new String[] { prevprevDay, today};
	}
}
